<?php

class MpController extends Zend_Controller_Action
{   
	function indexAction()
	{
		echo "Invalid Action.";
		die;
	}
	
	function sendContractNotificationMailsAction()
	{
		$dealers_db = Zend_Registry::get('dealers_db');
		
		$select = $dealers_db->select();
		$select->from("b_logs_mail_sent", "*");
		$select->where("status = ?", 0);
		$rows = $dealers_db->fetchAll($select);
		
		if(!empty($rows))
		{
			foreach($rows as $row)
			{
				$to = $row['mail_address'];
				$body = iconv("utf-8","gb2312//IGNORE",$row['contents']);
				$subjet = iconv("utf-8","gb2312//IGNORE",$row['title']);
				
				$mod_mail = new Sys_Mail();
				$mod_mail->sendMail($to, $subjet, $body);
				
				$set = array(
					"status" => 1,
					"updated_time" => date("Y-m-d H:i:s"),
					"try_times" => $row['try_times'] + 1
				);
				
				$where = "log_id=".$row['log_id'];
				
				$dealers_db->update("b_logs_mail_sent", $set, $where);
			}
		}
		
		echo "End.";
		die;
	}
	
	function portUserDealersAction()
	{
		$result = array(
			"result" => FALSE,
			"error_msg" => "",
			"data" => NULL
		);
		
		//get posted data
    	$input = file_get_contents("php://input"); //接收提交的所有POST数据
		$postStr = urldecode($input);//对提交的POST数据解码
		
		if ($postStr){
			$data = Zend_Json::decode($postStr);
			
			$emp_id = trim($data['emp_id']);
			
			if($emp_id)
			{
				$dealers_db = Zend_Registry::get('dealers_db');

				$select = $dealers_db->select();
				$select->from("b_bu_auth", "*");
				$select->where("emp_id = ?", $emp_id);
				$row = $dealers_db->fetchRow($select);
				
				if($row)
				{
					$mod_user = new Db_Tables2_User();
					$dealer_ids = $mod_user->MyHxbDealerIds($emp_id);
					
					$result['result'] = TRUE;
					$result['data'] = $dealer_ids;
				}else{
					$result['error_msg'] = "未设置用户权限";
				}
				
//				$ext = new Sys_Extensions();
//				$ext->FormatArray($a);
//				die;
			}
		}
		
		echo Zend_Json::encode($result);
		die;
	}
	
	function removeExpiredContractsAction()
	{
		$dealers_db = Zend_Registry::get('dealers_db');
		
		$set = array(
			"status" => 7
		);
		
		$today = date("Y-m-d");
		
		$where = "pay_type=1 and received_value < 1 and start_date < '".$today."' and `status` != 7";
		
		$result = $dealers_db->update("b_contract", $set, $where);
		
		var_dump($result);
		die;
	}
	
	function portArticlePublishTimeAction()
	{
		$result = array(
			"result" => FALSE,
			"error_msg" => "",
			"factories" => array(),
			"current_factory_id" => FALSE,
			"data" => array()
		);
		
		//get posted data
    	$input = file_get_contents("php://input"); //接收提交的所有POST数据
		$postStr = urldecode($input);//对提交的POST数据解码
		
//		$postStr = Zend_Json::encode(array(
//			"date" => "2018-12-07",
//			"dealer_id" => 2094414,
//			"city_id" => 440100
//		));
		
		if ($postStr){
			$data = Zend_Json::decode($postStr);
			
			$date_start = $data['date_start'];
			$date_end = $data['date_end'];
			$dealer_id = $data['dealer_id'];
			$city_id = $data['city_id'];
			$factory_id = $data['factory_id'];
			
			if($date_start && $date_end && $dealer_id && $city_id)
			{
				$cp_system_v2 = Zend_Registry::get('cp_system_v2');

				$select = $cp_system_v2->select();
				$select->from("t_autohome_article_publish_time", array("factoryId"));
				$select->where("dealerId = ?", $dealer_id);
				$select->where("cityId = ?", $city_id);
				$select->where("publishTime >= ?", $date_start." 00:00:00");
				$select->where("publishTime <= ?", $date_end." 23:59:59");
				$select->group("factoryId");
				$rows = $cp_system_v2->fetchAll($select);
				
				if($rows)
				{
					$factory_ids = array();
					
					foreach($rows as $row)
					{
						$factory_ids[] = $row['factoryId'];
					}
					
					if(!empty($factory_ids))
					{
						$cars_db = Zend_Registry::get('cars_db');
						
						$select = $cars_db->select();
						$select->from("t_factory", array("id", "name"));
						$select->where("id IN (?)", $factory_ids);
						$rows = $cars_db->fetchAll($select);
						
						if($rows)
						{
							foreach($rows as $row)
							{
								if(!$factory_id)
								{
									$factory_id = $row['id'];
								}
								
								$result['factories'][$row['id']] = $row['name'];
							}
							
							$result['current_factory_id'] = $factory_id;
							
							$result['result'] = TRUE;
							
							//$factory_id $dealer_id $city_id $date 都有了
							
							$select = $cp_system_v2->select();
							$select->from("t_autohome_article_publish_time", array("dealerId", "dealerName", "publishTime", "url"));
							$select->where("cityId = ?", $city_id);
							$select->where("publishTime >= ?", $date_start." 00:00:00");
							$select->where("publishTime <= ?", $date_end." 23:59:59");
							$select->where("factoryId IN (?)", $factory_ids);
							$rows = $cp_system_v2->fetchAll($select);
							
							if($rows)
							{
								foreach($rows as $row)
								{
									$time_key = intval(substr($row['publishTime'], 11, 2));
									$time_val = substr($row['publishTime'], 5, 11);
									
									if(!isset($result['data'][$row['dealerId']]))
									{
										$result['data'][$row['dealerId']] = array(
											"dealer_name" => $row['dealerName'],
											"timelist" => array(
												$time_key => array($time_val => $row['url'])
											)
										);
									}elseif(!isset($result['data'][$row['dealerId']]['timelist'][$time_key])){
										$result['data'][$row['dealerId']]['timelist'][$time_key] = array($time_val => $row['url']);
									}else{
										$result['data'][$row['dealerId']]['timelist'][$time_key][$time_val] = $row['url'];
									}
								}
							}
						}
					}
				}
				
//				$ext = new Sys_Extensions();
//				$ext->FormatArray($result);
//				die;
			}
		}
		
		echo Zend_Json::encode($result);
		die;
	}
	
	function portGetSpecsAction()
	{
		$result = array(
			"result" => FALSE,
			"error_msg" => "",
			"data" => array()
		);
		
		//get posted data
    	$input = file_get_contents("php://input"); //接收提交的所有POST数据
		$postStr = urldecode($input);//对提交的POST数据解码
		
		if ($postStr){
			$data = Zend_Json::decode($postStr);
			
			if(isset($data['factory_id']) || isset($data['series_id']))
			{
				$result['result'] = TRUE;
				
				$cars_db = Zend_Registry::get('cars_db');
				
				if($data['factory_id'])
				{
					$result['data']['series_info'] = array();
					
					$select = $cars_db->select();
					$select->from("t_spec", array("seriesId", "seriesName"));
					$select->where("factoryId = ?", $data['factory_id']);
					$select->group("seriesId");
					$select->order("seriesName ASC");
					$rows = $cars_db->fetchAll($select);
					
					if(!empty($rows))
					{
						foreach($rows as $row)
						{
							$result['data']['series_info'][$row['seriesId']] = $row['seriesName'];
						}
					}
				}
				
				if($data['series_id'])
				{
					$result['data']['spec_info'] = array();
					
					$select = $cars_db->select();
					$select->from("t_spec", array("id", "model"));
					$select->where("seriesId = ?", $data['series_id']);
					$select->order("model ASC");
					$rows = $cars_db->fetchAll($select);
					
					if(!empty($rows))
					{
						foreach($rows as $row)
						{
							$result['data']['spec_info'][$row['id']] = $row['model'];
						}
					}
				}
			}else{
				$result['error_msg'] = "key params missed.";
			}
		}
		
		echo Zend_Json::encode($result);
		die;
	}
	
	function portGetAllSpecsAction()
	{
		$result = array(
			"result" => FALSE,
			"error_msg" => "",
			"data" => array()
		);
		$params = $this->_request->getParams(); 
		 
        if($_GET["action"] == "series"){  		
			$result['result'] = TRUE;
			$cars_db = Zend_Registry::get('cars_db');
			$result['data']['series_info'] = array();

			$select = $cars_db->select();
			$select->from("t_series", array("_seriesId", "name"));
			$select->order("id ASC");
			$rows = $cars_db->fetchAll($select);
			if(!empty($rows))
			{
				foreach($rows as $row)
				{
					$result['data']['series_info'][$row['_seriesId']] = $row['name'];
				}
			}
			$result['error_msg'] = "success";		 
			
		}else{
			$result['error_msg'] = "key params missed.";
		}		
		echo Zend_Json::encode($result);
		die;
	}
			
	function portDealerIdInSameCityAction()
	{
		$result = array(
			"result" => FALSE,
			"error_msg" => "",
			"data" => array()
		);
		
		//get posted data
    	$input = file_get_contents("php://input"); //接收提交的所有POST数据
		$postStr = urldecode($input);//对提交的POST数据解码
		
		if ($postStr){
			$data = Zend_Json::decode($postStr);
			
			if(isset($data['dealer_id']))
			{
				$result['result'] = TRUE;
				
				$dealers_db = Zend_Registry::get('dealers_db');
				
				if($data['dealer_id'])
				{
					$select = $dealers_db->select();
					$select->from("t_dealer", array("cityId"));
					$select->where("_dealerId = ?", $data['dealer_id']);
					$row = $dealers_db->fetchRow($select);
					
					if($row)
					{
						if($row['cityId'])
						{
							$select = $dealers_db->select();
							$select->from("t_dealer", array("_dealerId"));
							$select->where("cityId = ?", $row['cityId']);
							$drows = $dealers_db->fetchAll($select);
							
							if(!empty($drows))
							{
								foreach($drows as $drow)
								{
									$result['data'][] = $drow['_dealerId'];
								}
							}
						}
					}
				}
			}else{
				$result['error_msg'] = "key params missed.";
			}
		}
		
		echo Zend_Json::encode($result);
		die;
	}
	
	function portGetBrandIdAction()
	{
		$params = $this->_request->getParams();
		
		$result = 0;
		
		$brand_name = urldecode($params['brand_name']);
		
		if($brand_name)
		{
			$cars_db = Zend_Registry::get('cars_db');
			
			$select = $cars_db->select();
			$select->from("t_brand", array("_brandId"));
			$select->where("name = ?", $brand_name);
			$row = $cars_db->fetchRow($select);
			
			if($row)
			{
				$result = $row['_brandId'];
			}
		}
		
		echo $result;
		die;
	}
	
	function portDealerLocationAction()
	{
		$result = array(
			"result" => FALSE,
			"error_msg" => "",
			"data" => array()
		);
		
		//get posted data
    	$input = file_get_contents("php://input"); //接收提交的所有POST数据
		$postStr = urldecode($input);//对提交的POST数据解码
		
		if ($postStr){
			$data = Zend_Json::decode($postStr);
			
			if(isset($data['dealer_id']))
			{
				$result['result'] = TRUE;
				
				$dealers_db = Zend_Registry::get('dealers_db');
				
				if($data['dealer_id'])
				{
					$select = $dealers_db->select();
					$select->from("t_dealer", array("provinceId", "provinceName", "cityId", "cityName", "countyId", "countyName"));
					$select->where("id = ?", $data['dealer_id']);
					$row = $dealers_db->fetchRow($select);
					
					if($row)
					{
						$result['data']['provinceId'] = $row['provinceId'];
						$result['data']['provinceName'] = $row['provinceName'];
						$result['data']['cityId'] = $row['cityId'];
						$result['data']['cityName'] = $row['cityName'];
						$result['data']['countyId'] = $row['countyId'];
						$result['data']['countyName'] = $row['countyName'];
					}
				}
			}else{
				$result['error_msg'] = "key params missed.";
			}
		}else{
			$result['error_msg'] = "no post params";
		}
		
		echo Zend_Json::encode($result);
		die;
	}

    public function clientsExportAction()
    {
		set_time_limit(0);
		
        $params = $this->_request->getParams();
        $objPHPExcel = new PHPExcel();
		$mod_logs_contract = new Db_Tables2_BLogsContract();
		
		$dealers_db = Zend_Registry::get("dealers_db");
		
		//获取所有产生过合同的客户ID
		$select = $dealers_db->select();
		$select->from("b_contract", "client_id");
		$rows = $dealers_db->fetchAll($select);
		
		$available_client_ids = array();
		
		foreach($rows as $row)
		{
			$available_client_ids[] = $row['client_id'];
		}
		
		$available_client_ids = array_unique($available_client_ids);
		
		//获取所有客户
		$select = $dealers_db->select();
		$select->from("b_client as c", array("client_id", "client_name", "tel", "fax", "cellphone", "province_name", "city_name", "county_name", "address", "zipcode", "is_dealer"));
		$select->joinLeft("b_client_type as t", "t.client_type_id=c.client_type_id", array("client_type_id", "name as client_type_name"));
		$select->joinLeft("b_client_factory as cf", "cf.client_id=c.client_id", array());
		$select->joinLeft("cars_db.t_factory as f", "f.id=cf.factory_id", array("name as factory_name", "letter as factory_letter"));
		$select->joinLeft("hxb_db.auth_emp as a", "a.id=c.emp_id", array("realName"));
		$select->where("c.client_id IN (?)", $available_client_ids);
//		$select->limit(100);
		$rows = $dealers_db->fetchAll($select);
//		var_dump(count($rows));die;
		$mod_ext = new Sys_Extensions();
//		$mod_ext->FormatArray($rows);
//		die;
		
        if(!empty($rows))
        {
			$columns = $mod_ext->ExcelColumnHeadGeneration(200);
			
			$column_ct = 0;
			
            //title
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].'1', '客户名称(必填)');
			$column_ct += 1;
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].'1', '客户类型(必填)');
			$column_ct += 1;
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].'1', '上级客户');
			$column_ct += 1;
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].'1', '电话(必填)');
			$column_ct += 1;
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].'1', '邮箱');
			$column_ct += 1;
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].'1', '传真');
			$column_ct += 1;
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].'1', '网址');
			$column_ct += 1;
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].'1', '省(必填)');
			$column_ct += 1;
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].'1', '市(必填)');
            $column_ct += 1;
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].'1', '区');
			$column_ct += 1;
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].'1', '地址(必填)');
			$column_ct += 1;
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].'1', '邮编');
			$column_ct += 1;
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].'1', '跟进状态(必填)');
			$column_ct += 1;
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].'1', '客户来源');
			$column_ct += 1;
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].'1', '人员规模');
			$column_ct += 1;
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].'1', '下次跟进时间');
			$column_ct += 1;
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].'1', '备注');
			$column_ct += 1;
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].'1', '厂商品牌(必填)');
			$column_ct += 1;
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].'1', '系统对接id');
			$column_ct += 1;
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].'1', '税号');
			$column_ct += 1;
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].'1', '注册地址');
            $column_ct += 1;
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].'1', '开票电话');
            $column_ct += 1;
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].'1', '开票开户银行');
            $column_ct += 1;
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].'1', '开票银行账号');
            $column_ct += 1;
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].'1', '负责人');
            $column_ct += 1;
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].'1', '联系人姓名');
            $column_ct += 1;
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].'1', '联系人电话');
            $column_ct += 1;
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].'1', '联系人手机');
            $column_ct += 1;
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].'1', 'img1');
            $column_ct += 1;
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].'1', 'img2');
            $column_ct += 1;
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].'1', 'img3');
            $column_ct += 1;
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].'1', 'img4');

            $n = 2;
			
            foreach($rows as $result)
            {
				$column_ct = 0;
				
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, isset($result["client_name"])?$result["client_name"]:"");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, isset($result["client_type_name"])?$result["client_type_name"]:"");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, "");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, isset($result["tel"])?$result["tel"]:"");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, "");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, isset($result["fax"])?$result["fax"]:"");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, "");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, isset($result["province_name"])?$result["province_name"]:"");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, isset($result["city_name"])?$result["city_name"]:"");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, isset($result["county_name"])?$result["county_name"]:"");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, isset($result["address"])?$result["address"]:"");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, isset($result["zipcode"])?$result["zipcode"]:"");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, "");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, "");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, "");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, "");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, "");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, isset($result["factory_name"])?($result["factory_letter"]."-".$result["factory_name"]):"");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, isset($result["is_dealer"])?$result["is_dealer"]:"");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, "");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, "");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, "");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, "");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, "");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, isset($result["realName"])?$result["realName"]:"");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, "");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, "");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, isset($result["cellphone"])?$result["cellphone"]:"");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, "");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, "");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, "");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, "");$column_ct += 1;

                $n += 1;
            }
			
			//签约公司
			$select = $dealers_db->select();
			$select->from("b_signer as s", array("company_name", "tel", "fax", "province_name", "city_name", "contact_address", "zipcode", "signer_id", "tax_number", "register_address", "bank_name", "bank_number", "company_owner", "img1", "img2", "img3", "img4"));
			$select->joinLeft("hxb_db.auth_emp as a", "a.id=s.emp_id", array("realName"));
//			$select->limit(20);
			$rows = $dealers_db->fetchAll($select);
			
			foreach($rows as $result)
            {
				$column_ct = 0;
				
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, isset($result["company_name"])?$result["company_name"]:"");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, "签约公司");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, "");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, isset($result["tel"])?$result["tel"]:"");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, "");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, isset($result["fax"])?$result["fax"]:"");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, "");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, isset($result["province_name"])?$result["province_name"]:"");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, isset($result["city_name"])?$result["city_name"]:"");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, "");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, isset($result["contact_address"])?$result["contact_address"]:"");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, isset($result["zipcode"])?$result["zipcode"]:"");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, "");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, "");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, "");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, "");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, "");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, "");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, isset($result["signer_id"])?$result["signer_id"]:"");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, isset($result["tax_number"])?$result["tax_number"]:"");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, isset($result["register_address"])?$result["register_address"]:"");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, "");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, isset($result["bank_name"])?$result["bank_name"]:"");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, isset($result["bank_number"])?$result["bank_number"]:"");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, isset($result["realName"])?$result["realName"]:"");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, isset($result["company_owner"])?$result["company_owner"]:"");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, "");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, "");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, isset($result["img1"])?("http://biz.sys.xiongmaocar.com/upload/signers/".$result["img1"]):"");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, isset($result["img2"])?("http://biz.sys.xiongmaocar.com/upload/signers/".$result["img2"]):"");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, isset($result["img3"])?("http://biz.sys.xiongmaocar.com/upload/signers/".$result["img3"]):"");$column_ct += 1;
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($columns[$column_ct].$n, isset($result["img4"])?("http://biz.sys.xiongmaocar.com/upload/signers/".$result["img4"]):"");$column_ct += 1;

                $n += 1;
            }
        }
		
		$rd = mt_rand(1000, 9999);
		$folder = "upload/reports/";
        $file_name = "export-client2-".date("YmdHis").$rd.".xlsx";
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
        $objWriter->save($folder.$file_name);
		
//        header("Location: /".$folder.$file_name);

        echo "<a href='/".$folder.$file_name."'>点击下载客户+签约公司列表</a>";
        die;

    }
	
	function portFactoryInfoAction()
	{
		$cars_db = Zend_Registry::get('cars_db');

		$select = $cars_db->select();
		$select->from("t_factory", array("id", "name"));
		$select->order("letter ASC");
		$rows = $cars_db->fetchAll($select);
		
		$result = array();
		
		if($rows)
		{
			foreach($rows as $row)
			{
				$r = array(
					"id" => $row['id'],
					"name" => $row['name']
				);
				
				$result[] = $r;
			}
		}
		
		echo Zend_Json::encode($result);
		die;
	}
	
	function portGetAccountManagerAction()
	{
		$result = array(
			"result" => FALSE,
			"error_msg" => "",
			"data" => array()
		);
		
		//get posted data
    	$input = file_get_contents("php://input"); //接收提交的所有POST数据
		$postStr = urldecode($input);//对提交的POST数据解码
		
		if ($postStr){
			$data = Zend_Json::decode($postStr);
			
			if(isset($data['user_id']))
			{
				$result['result'] = TRUE;
				
				$hxb_db = Zend_Registry::get('hxb_db');
		
				$select = $hxb_db->select();
				$select->from("auth_emp as e", array("realName"));
				$select->joinLeft("maozhua_wb.users as u", "e.id=u.emp_id", array());
				$select->where("u.user_id =?",$data['user_id']);
				$rows = $hxb_db->fetchRow($select);

				if($rows)
				{
					$result['data'] = array(
						"realName" => $rows['realName'],
					);
				}
			}else{
				$result['error_msg'] = "key params missed.";
			}
		}
		
		echo Zend_Json::encode($result);
		die;
	}
	
}